package com.aliang.shopping.service.impl;


import cn.hutool.core.util.ObjUtil;
import com.aliang.shopping.common.PageResult;
import com.aliang.shopping.common.ResultCodeEnum;
import com.aliang.shopping.config.OssConfig;
import com.aliang.shopping.contact.SqlColumnConstant;
import com.aliang.shopping.exception.DataException;
import com.aliang.shopping.exception.ServiceException;
import com.aliang.shopping.mapper.AdminMapper;
import com.aliang.shopping.model.converter.AdminConverter;
import com.aliang.shopping.model.converter.CommonConverter;
import com.aliang.shopping.model.dto.common.PageDTO;
import com.aliang.shopping.model.dto.login.EmailLoginDTO;
import com.aliang.shopping.model.dto.login.LoginDTO;
import com.aliang.shopping.model.dto.login.PhoneLoginDTO;
import com.aliang.shopping.model.po.Admin;
import com.aliang.shopping.model.vo.CommonUserInfoVO;
import com.aliang.shopping.model.vo.admin.AdminExportVO;
import com.aliang.shopping.model.vo.admin.AdminVO;
import com.aliang.shopping.security.auth.Payload;
import com.aliang.shopping.service.AdminService;
import com.aliang.shopping.util.PasswordUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ShiBaLover
 * @description 针对表【admin】的数据库操作Service实现
 * @createDate 2024-10-09 09:34:18
 */
@Service
@AllArgsConstructor
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
        implements AdminService {
    private final CommonConverter<AdminVO> commonConverter = new CommonConverter<>();

    private final AdminConverter adminConverter;
    private final OssConfig ossConfig;

    @Override
    public PageResult<AdminVO> selectPage(PageDTO pageDTO) {
        //查询数据
        IPage<AdminVO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        IPage<AdminVO> mapperPage = this.baseMapper.getPage(page, pageDTO);
        //转换数据
        mapperPage.getRecords().forEach(vo -> {
            if (ObjUtil.isNotNull(vo.getAvatar()))
                vo.setAvatar(ossConfig.getUrl() + vo.getAvatar());
        });
        return commonConverter.toPageResult(mapperPage);
    }

    @Override
    public List<AdminExportVO> getExportList(PageDTO pageDTO) {
        List<AdminVO> list = this.baseMapper.getVoList(pageDTO);
        if (list.isEmpty()) {
            throw new DataException(ResultCodeEnum.NOT_QUERY_RESULT);
        }
        //转换数据
        //转换数据
        list.forEach(vo -> {
            if (ObjUtil.isNotNull(vo.getAvatar()))
                vo.setAvatar(ossConfig.getUrl() + vo.getAvatar());
        });
        return adminConverter.toImportDTOList(list);
    }

    /**
     * 登录方法
     *
     * @param loginDTO
     * @return
     */
    @Override
    public Payload login(LoginDTO loginDTO) {
        //根据用户名查询用户信息
        Admin admin = this.baseMapper.selectOne(new QueryWrapper<Admin>().eq(SqlColumnConstant.USERNAME, loginDTO.getUsername())
                .eq(SqlColumnConstant.STATUS, SqlColumnConstant.STATUS_OK));
        //判断是否为空
        if (ObjUtil.isEmpty(admin)) {
            throw new DataException(ResultCodeEnum.USERNAME_IS_NOT_EXIT);
        }
        // 判断密码是否一致
        if (PasswordUtil.isNotEqual(admin.getPassword(), loginDTO.getPassword())) {
            throw new DataException(ResultCodeEnum.PASSWORD_NOT_MATCH);
        }
        return new Payload(admin.getId(), CommonConverter.getAdminRoleNum(admin.getTypeNum()));
    }


    @Override
    public Payload emailLogin(EmailLoginDTO loginDTO) {
        Admin admin = this.baseMapper.selectOne(new QueryWrapper<Admin>().eq(SqlColumnConstant.EMAIL, loginDTO.getEmail())
                .eq(SqlColumnConstant.STATUS, SqlColumnConstant.STATUS_OK));
        if (!ObjUtil.isEmpty(admin)) {
            //返回载荷
            return new Payload(admin.getId(), CommonConverter.getAdminRoleNum(admin.getTypeNum()));
        }
        return null;
    }

    @Override
    public Payload phoneLogin(PhoneLoginDTO loginDTO) {
        Admin admin = this.baseMapper.selectOne(new QueryWrapper<Admin>().eq(SqlColumnConstant.PHONE, loginDTO.getPhone())
                .eq(SqlColumnConstant.STATUS, SqlColumnConstant.STATUS_OK));
        if (!ObjUtil.isEmpty(admin)) {
            //返回载荷
            return new Payload(admin.getId(), CommonConverter.getAdminRoleNum(admin.getTypeNum()));
        }
        return null;
    }

    @Override
    public CommonUserInfoVO getUserInfo(Long id) {
        Admin admin = this.baseMapper.selectById(id);
        if (ObjUtil.isEmpty(admin)) {
            throw new ServiceException(ResultCodeEnum.USER_NOT_LOGIN);
        }
        return this.adminConverter.toUserInfoVO(admin);
    }
}




